//
//  OKGetCodeBtn.m
//  易阅小说
//
//  Created by OKweixin2 on 2017/7/12.
//  Copyright © 2017年 OKboxun--weixin. All rights reserved.
//

#import "OKGetCodeBtn.h"

@interface OKGetCodeBtn()

@property (nonatomic, assign) NSInteger count;
@property (nonatomic,copy)UIColor *color;

@end

@implementation OKGetCodeBtn

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.strTime = @"重新获取";
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}

- (void)setup {
    
    [self setTitle:@" 获取验证码 " forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:11];
    self.backgroundColor = [UIColor clearColor];
    
//    [self setTitleColor:[UIColor colorWithHex:@"0x2cd0ca"] forState:UIControlStateNormal];
    [self setTitleColor:baseColorTheme forState:UIControlStateNormal];

}

- (void)timeFailBeginFrom:(NSInteger)timeCount {
    
    self.count = timeCount;
    self.enabled = NO;
    // 加1个计时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    [self setTitleColor:[UIColor colorWithHex:@"999999"] forState:UIControlStateNormal];

}

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
        self.enabled = NO;
//        NSLog(@"%@",[NSString stringWithFormat:@"剩余%ld秒", self.count]);
        [self setTitleColor:[UIColor colorWithHex:@"999999"] forState:UIControlStateNormal];
        [self setTitle:[NSString stringWithFormat:@"%@ %dS",self.strTime, self.count] forState:UIControlStateNormal];
    } else {
        
        self.enabled = YES;
        if (self.strTime.length == 0) {
            [self setTitle:@"获取" forState:UIControlStateNormal];
        }else{
            [self setTitle:@"重新获取" forState:UIControlStateNormal];
        }
        self.alpha = 1;
        [self setTitleColor:baseColorTheme forState:UIControlStateNormal];
        [self.timer invalidate];
    }
}


@end
